package com.car.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.car.commons.core.model.Result;
import com.car.order.client.CarAccountClient;
import com.car.order.client.model.account.admin.AdminDto;
import com.car.order.client.model.clerk.ClerkDto;
import com.car.order.dto.goodOrderLog.GoodOrderLogDto;
import com.car.order.entity.form.goodOrderLog.GoodOrderLogFindListForm;
import com.car.order.entity.form.goodOrderLog.GoodOrderLogFindPageForm;
import com.car.order.entity.form.goodOrderLog.GoodOrderLogInsertForm;
import com.car.order.entity.form.goodOrderLog.GoodOrderLogUpdateForm;
import com.car.order.mapper.GoodOrderLogMapper;
import com.car.commons.core.model.WoniuException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.order.entity.GoodOrderLog;
import com.car.order.service.GoodOrderLogService;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 杜岷芫
 * @since 2024-08-28 19:25:28
 */
@Service
@Slf4j
public class GoodOrderLogServiceImpl extends ServiceImpl<GoodOrderLogMapper, GoodOrderLog> implements GoodOrderLogService {

    @Resource
    private GoodOrderLogMapper goodOrderLogMapper;
    @Resource
    private CarAccountClient carAccountClient;

    @Override
    public Page<GoodOrderLog> findPage(GoodOrderLogFindPageForm form) {
        Page<GoodOrderLog> page = new Page<>(form.getPageNum()==null?1:form.getPageNum(),form.getPageSize()==null?6:form.getPageSize());
        QueryWrapper<GoodOrderLog> query = new QueryWrapper<>();
        return goodOrderLogMapper.selectPage(page, query);
    }

    @Override
    public List<GoodOrderLogDto> findList(GoodOrderLogFindListForm form){
        QueryWrapper<GoodOrderLog> query = new QueryWrapper<>();
        query.eq("good_order_id", form.getOrderId());
        List<GoodOrderLog> goodOrderLogs = goodOrderLogMapper.selectList(query);
        Set<Integer> clerkIds = goodOrderLogs.stream().map(GoodOrderLog::getLogUser).filter(o->o!=null).collect(Collectors.toSet());
        System.out.println("clerkIds"+clerkIds);
        HashMap<Integer, AdminDto> clerkDtoHashMap = new HashMap<>();
        if (clerkIds.size() != 0) {
            Result<List<AdminDto>> clerksResult = carAccountClient.findsAdminByIds(clerkIds);
            if (clerksResult.getCode() != 200) {
                throw new WoniuException(clerksResult.getCode(), clerksResult.getMsg());
            }
            List<AdminDto> clerkDtos = clerksResult.getData();
            if (clerkDtos == null || clerkDtos.size() != clerkIds.size()) {
                throw new WoniuException(500, "有管理员没查到");
            }
            for (AdminDto clerkDto : clerkDtos) {
                clerkDtoHashMap.put(clerkDto.getAdminId(), clerkDto);
            }
        }

        List<GoodOrderLogDto> goodOrderLogDtos = new ArrayList<>();
        for (GoodOrderLog goodOrderLog : goodOrderLogs) {
            GoodOrderLogDto goodOrderLogDto = BeanUtil.toBean(goodOrderLog, GoodOrderLogDto.class);
            AdminDto clerkDto = clerkDtoHashMap.get(goodOrderLog.getLogUser());
            goodOrderLogDto.setLogUserName(clerkDto==null?"用户":clerkDto.getAdminName());
            goodOrderLogDtos.add(goodOrderLogDto);
        }
        return goodOrderLogDtos;
    }

    @Override
    public GoodOrderLog findById(Integer id) {
        return goodOrderLogMapper.selectById(id);
    }

    @Override
    public GoodOrderLog insert(GoodOrderLogInsertForm form) {
        GoodOrderLog goodOrderLog = BeanUtil.toBean(form, GoodOrderLog.class);
        boolean save = save(goodOrderLog);
        if (!save) {
            throw new WoniuException(500, "新增失败");
        }
        return goodOrderLog;
    }

    @Override
    public GoodOrderLog update(GoodOrderLogUpdateForm form) {
        GoodOrderLog goodOrderLog = BeanUtil.toBean(form, GoodOrderLog.class);
        boolean b = updateById(goodOrderLog);
        if (!b) {
            throw new WoniuException(500, "修改失败");
        }
        return goodOrderLog;
    }

    @Override
    public void delete(Integer id) {
        int i=goodOrderLogMapper.deleteById(id);
        if (i == 0) {
            throw new WoniuException(500, "删除失败");
        }
    }
}